package org.example.modules.user.controller;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.example.common.utils.ThreadLocalUtil;
import org.example.modules.user.common.Commsult;
import org.example.modules.user.entry.User;
import org.example.modules.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description LoginController
 * @Author xiaotian
 * @Date 2025-03-02
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;

//    @PostMapping("/wxLogin")
//    public ResponseEntity<LoginResponse> wxLogin(@RequestBody JSONObject loginParam) {
//        String code = loginParam.getString("code");
//        log.info("loginInfo:{}",loginParam);
//        LoginResponse response = userService.wxLogin(code);
//        return ResponseEntity.ok(response);
//    }
    @PostMapping("/wxLogin")
    public Commsult wxLogin(@RequestBody JSONObject loginParam) {
        Map map = new HashMap<>();
        String str  =userService.wxLogin(loginParam);
        if(str.equals("用户名或密码不正确")||str.equals("该账号已被冻结请联系管理员")){
            return Commsult.fail(str);
        }
        map.put("token",str);
        return Commsult.success(map);
    }
    @PostMapping("/updatePassword")
    public Commsult userUpdatePassword(@RequestBody JSONObject data) {
        ;
        return userService.updatePassword(data);
    }
    @PostMapping("/resetPassword")
    public Commsult resetPassword(@RequestBody JSONObject data) {
        ;
        return userService.resetPassword(data);
    }
    @PostMapping("/updateUserInfo")
    public Commsult userUpdateInfo(@RequestBody JSONObject data) {
        userService.updateUserInfo(data);
        return Commsult.success(null);
    }

    @PostMapping("/register")
    public Object userRegister(@RequestBody JSONObject data){
        return userService.userRegister(data);
    }

    @GetMapping
    public Commsult<List<User>> getAllUser( @RequestParam(value = "id", required = false) String id){
        return Commsult.success(userService.getUserList());
    }
    @PostMapping("updateStatus")
    public Commsult updateStatus(@RequestBody JSONObject data){
        userService.updateUserStatus(data);
        return Commsult.success(null);
    }

}